<?php

class Silk_Zgb_Block_Adminhtml_Rma_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

		public function __construct()
		{
				parent::__construct();
				$this->setId("rmaGrid");
				$this->setDefaultSort("id");
				$this->setDefaultDir("DESC");
				$this->setSaveParametersInSession(true);

		}

		protected function _prepareCollection()
		{
				$collection = Mage::getModel("zgb/rma")->getCollection();
				$select = $collection->getSelect();

				$lastname  = Mage::getResourceModel('customer/customer')->getAttribute('lastname');
		        $tablename = $lastname->getBackend()->getTable();
		        $attributeId = $lastname->getAttributeId();
		        $select->joinLeft(array('ce' => $tablename), "ce.entity_id=main_table.customer_id and ce.attribute_id='{$attributeId}'", array('customer_name'=>'ce.value'));
		        $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
		        if($websiteId){
		            $storeId = Mage::getModel('core/website')->load($websiteId)->getDefaultStore()->getId();
		            $select->join(array('sfo'=>'sales_flat_order'), "main_table.order_id=sfo.entity_id And sfo.store_id=".$storeId, array('increment_id'=>'sfo.increment_id'));
		        }else{
		        	$select->joinLeft(array('sfo'=>'sales_flat_order'), "main_table.order_id=sfo.entity_id", array('increment_id'=>'sfo.increment_id'));
		        }
		     
		        $collection->addFilterToMap('customer_name', 'ce.value');
		        $collection->addFilterToMap('increment_id', 'sfo.increment_id');
				$this->setCollection($collection);
				return parent::_prepareCollection();
		}
		protected function _prepareColumns()
		{
			$this->addColumn("id", array(
				"header" => Mage::helper("zgb")->__("ID"),
				"align" =>"right",
				"width" => "50px",
			    "type" => "number",
				"index" => "id",
			));
            
			$this->addColumn('created_at', array(
	            'header' => Mage::helper('sales')->__('请求时间'),
	            'index' => 'created_at',
	            'filter_index'=>'main_table.created_at',
	            'type' => 'datetime',
	            'width' => '180px',
	        ));

            $this->addColumn("customer_name", array(
				"header" => Mage::helper("zgb")->__("客户名"),
				"index" => "customer_name",
			));

			$this->addColumn("increment_id", array(
				"header" => Mage::helper("zgb")->__("订单号"),
				"index" => "increment_id",
			));

			$this->addColumn('status', array(
	            'header' => Mage::helper('sales')->__('Status'),
	            'index' => 'status',
	            'filter_index'=>'main_table.status',
	            'type'  => 'options',
	            'width' => '70px',
	            'options' => array('0'=>'待处理','1'=>'已处理')
	        ));

	        $this->addColumn('updated_at', array(
	            'header' => Mage::helper('sales')->__('处理时间'),
	            'index' => 'updated_at',
	            'filter_index'=>'main_table.updated_at',
	            'type' => 'datetime',
	            'width' => '180px',
	        ));
			
			//$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV')); 
			//$this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel'));

			return parent::_prepareColumns();
		}

		public function getRowUrl($row)
		{
			   return '#';//$this->getUrl("*/*/edit", array("id" => $row->getId()));
		}


		
		protected function _prepareMassaction()
		{
			$this->setMassactionIdField('id');
			$this->getMassactionBlock()->setFormFieldName('ids');
			$this->getMassactionBlock()->setUseSelectAll(true);
			$this->getMassactionBlock()->addItem('confirm', array(
					 'label'=> Mage::helper('zgb')->__('已处理'),
					 'url'  => $this->getUrl('*/adminhtml_rma/massConfirm'),
					 'confirm' => Mage::helper('zgb')->__('Are you sure?')
				));
			return $this;
		}
			

}